package com.tms.app.tms.ui.base.ui;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tms.app.tms.R;
import com.tms.app.tms.bean.response.BaseResponse;
import com.tms.app.tms.bean.response.ErrorMsgRes;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by happy on 2017/8/26.
 */

public abstract class AbsFragment extends Fragment {
    public List list=new ArrayList<>();
    public View rootView;
    @Override
    public void onResume() {
        super.onResume();
        if (!EventBus.getDefault().isRegistered(this))
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    public void showMsg(String msg) {
        Toast.makeText(getContext(),msg, Toast.LENGTH_SHORT).show();
    }

    public TextView toolbarLeft;
    public TextView toolbarCenter;
    public TextView toolbarRight;

    private  void  initViewToolBar(View view){
        try {
            toolbarCenter= (TextView)view. findViewById(R.id.toolbar_center);
            toolbarLeft= (TextView)view.  findViewById(R.id.toolbar_left);
            toolbarRight= (TextView) view. findViewById(R.id.toolbar_right);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void setToolBarTitle(View view,String title,int left,int right){
        if (toolbarLeft==null||toolbarRight==null||toolbarCenter==null)
            initViewToolBar(view);
        if (toolbarCenter!=null)
            toolbarCenter.setText(title);
        if (toolbarLeft!=null&&left!=-1){
            if (left==0)
                toolbarLeft.setVisibility(View.INVISIBLE);
            else
                toolbarLeft.setBackgroundResource(left);
        }

        if (toolbarRight!=null&&right!=-1){
            if (right==0)
                toolbarRight.setVisibility(View.INVISIBLE);
            else
                toolbarRight.setBackgroundResource(right);
        }


    }

    public void setToolBarLisenter(View view,View.OnClickListener listener,View.OnClickListener listener2){
        if (toolbarLeft==null||toolbarRight==null||toolbarCenter==null)
            initViewToolBar(view);

        if (toolbarLeft!=null)
            toolbarLeft.setOnClickListener(listener);
        if (toolbarRight!=null)
            toolbarRight.setOnClickListener(listener2);
    }


    public void showNetError(){
        showNetError(0);
    }
    public void showNetError(int imglevel){
       showNetError(R.color.colorPrimary,0,"网络异常");
    }
    public void showNetError(int color,int imglevel,String msg){
        try {
           rootView. findViewById(R.id.net_errot_layout).setVisibility(View.VISIBLE);
            ImageView imageView=rootView.findViewById(R.id.img_net_error);
            TextView textView=rootView.findViewById(R.id.text_net_error);
            imageView.getBackground().setLevel(imglevel);
            textView.setTextColor(color);
            textView.setText(msg);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void hideNerError(){
        try {
            rootView. findViewById(R.id.net_errot_layout).setVisibility(View.GONE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void hideSoftInputFromWindow(Context context, View view){
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public <T extends BaseResponse> void onReceiveErroMsg(T t) {
        if (t instanceof ErrorMsgRes){
            ErrorMsgRes errorMsgRes= (ErrorMsgRes) t;
            handlerErrorMsg(errorMsgRes.getCode());
        }

    }

    public abstract void handlerErrorMsg(int errorCode);

}
